package com.fit.pages.presenceOnClasses;

import java.util.Date;
import java.util.List;

import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.internal.antlr.PropertyExpressionParser.list_return;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.ioc.services.PropertyAccess;
import org.apache.tools.ant.taskdefs.Sleep;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.fit.annotations.AdminAccess;
import com.fit.annotations.AnonymousAccess;
import com.fit.annotations.ProfessorAccess;
import com.fit.annotations.StudentAccess;
import com.fit.dao.presenceOnClasses.PresenceOnClassesDAO;
import com.fit.dao.studentOfTheCourse.StudentOfTheCourseDAO;
import com.fit.dao.classes.ClassesDAO;
import com.fit.dao.course.CourseDAO;
import com.fit.dao.language.LanguageDAO;
import com.fit.dao.user.UserDAO;
import com.fit.entities.presenceOnClasses.PresenceOnClasses;
import com.fit.entities.studentOfTheCourse.StudentOfTheCourse;
import com.fit.entities.classes.Classes;
import com.fit.entities.course.Course;
import com.fit.entities.language.Language;
import com.fit.entities.user.User;
import com.fit.lov.generic.GenericSelectModel;
import com.fit.lov.user.UserSelectModel;
import com.fit.pages.ErrorPage;
import com.fit.services.Authenticator;

@AdminAccess
@AnonymousAccess
@ProfessorAccess
@StudentAccess
public class Add {

	private GenericSelectModel<User> userModel;
	private GenericSelectModel<Classes> classesModel;

	// Inject
	@Inject
	private PropertyAccess access;
	@Inject
	private Session session;
	@Inject
	private Messages messages;
	@Inject
	private Authenticator authenticator;
	@Inject
	private UserDAO userDAO;
	@Inject
	private ClassesDAO classesDAO;
	@Property
	private PresenceOnClasses presenceOnClasses;	
	@Property
	private User user;
	@Property
	private Classes classes;


	@Persist
	@Property
	private User selectedUser;

	@Persist
	@Property
	private String comment;
	@Property
	@Persist
	private Classes selectedClasses;
	
	public GenericSelectModel<User> getUserModel(){
		List<User> s = session.createCriteria(User.class).add(Restrictions.sqlRestriction("role = 2")).list();
		userModel = new GenericSelectModel<User>(s, User.class, "userName", "id", access);
		return userModel;
	}
	
	
	public GenericSelectModel<Classes> getClassesModel() {
		List<Classes> c = classesDAO.findAll();
		classesModel = new GenericSelectModel<Classes>(c, Classes.class, "beginingDate", "classesId", access);
		return classesModel;
	}

	

	Object onSubmitFromAddTestForm()
	{
		System.out.println("The form was submitted!");
		return null;
	}


	@OnEvent(value="selected", component="submitButton")
	void onSubmitButton()
	{
		PresenceOnClasses c = new PresenceOnClasses();

		System.out.println(selectedClasses);
		c.setClasses(selectedClasses);
		c.setUser(selectedUser);
		c.setComment(comment);
		session.save(c);
		selectedClasses = null;
		selectedUser = null;
		comment=null;
	}

	@OnEvent(component = "ResetConfLink")
	Object onResetConfLink() {
		return null;
	}

	@OnEvent(component = "ResetErrorLink")
	Object onResetErrorLink() {
		return null;
	}



}
